package com.example.solrdemo.solrtest;

import com.example.solrdemo.entity.User;
import com.example.solrdemo.mapper.SolrMapper;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/solr")
public class SolrTest2 {
    public static Logger logger = LoggerFactory.getLogger(SolrTest2.class);
    @Autowired
    private SolrClient solrClient;
    @Autowired
    private SolrMapper solrMapper;
    //查询出数据库内容建立solr索引库
    @RequestMapping("/save")
    public String saveUser(){
        try {
            List<User> liset = solrMapper.queryUser();
            for(User user :liset){
                System.out.println(user.toString());
            }
            solrClient.addBeans(liset);
            solrClient.commit();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "索引库建立完成";
    }
    //单个新增数据库数据,更新索引库
    @RequestMapping("/add")
    public String addUser(){
        try {
            User user = new User();
            user.setId("6");
            user.setUsername("李寻欢");
            user.setAge(26);
            user.setSex("男");
            user.setMark(100);
            logger.info("单个新增数据库,更新索引库===>"+user.toString());
            solrMapper.insert(user);
            solrClient.addBean(user);
            solrClient.commit();
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("数据插入失败");
        }
        return "数据插入成功";
    }
    //根据id删除索引
    @RequestMapping("/delete/{id}")
    public String delUser(@PathVariable(value = "id") String id){
        try {
            solrClient.deleteById(id);
            solrClient.commit();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "删除成功";
    }
    //查询1:根据姓名查询
    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public List<User> queryByUsername(String keywords){
        try {
            //构建查询
            SolrQuery params = new SolrQuery();
            //添加查询条件
            params.setQuery("name:"+"*"+keywords+"*");
            //params.set("fq","age:"+11);
            //排序
            params.setSort("id", SolrQuery.ORDER.desc);
            //设置分页
            params.setStart(0);
            params.setRows(10);
            //执行查询
            QueryResponse response = solrClient.query(params);
            List<User> list = response.getBeans(User.class);
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


}
